package com.pro.framework.mq.rabbit;

import com.pro.framework.api.mq.IMqModule;
import com.pro.framework.api.mq.IMqRabbitMqRoutingKey;
import org.springframework.amqp.core.*;

import java.util.ArrayList;
import java.util.List;

// 2. 统一接口
public class MqRabbitUtil {

    public static Declarables getDeclarables(TopicExchange appExchange, IMqRabbitMqRoutingKey[] values, IMqModule moduleNow) {
        List<Declarable> decls = new ArrayList<>();

        for (IMqRabbitMqRoutingKey routingKey : values) {
            String routingKeyAndQueue = routingKey.getRoutingKeyAndQueue();
            List<Queue> queues = new ArrayList<>(16);
            //  如果当前服务是生产者/消费者 加载queue
            if (routingKey.getProviders().contains(moduleNow) || routingKey.getConsumer().equals(moduleNow)) {
                queues.add(QueueBuilder.durable(routingKeyAndQueue).build());
            }
            decls.addAll(queues);
            for (Queue queue : queues) {
                decls.add(BindingBuilder.bind(queue).to(appExchange).with(routingKeyAndQueue));
            }
        }
        return new Declarables(decls);
    }
}
